Page({
  data: {
    majorArr: [],
    majorIndex: 0,
    gradeArr: [],
    gradeIndex: 0,
  },
  onLoad: function () {
    this.fetchData()
  },
  fetchData: function () {
    var that = this;
    wx.request({
      url: 'http://localhost:8080/clock_war_exploded/major/findMajor',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function(res){
        if (res.data != null){
          that.setData({
            majorArr: res.data
          })
        }
      }
    })
    wx.request({
      url: 'http://localhost:8080/clock_war_exploded/major/findGrade',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function(res){
        if (res.data != null){
          that.setData({
            gradeArr: res.data
          })
        }
      }
    })
  },
  bindPickerChange: function (e) { //下拉选择
    const eindex = e.detail.value;
    const name = e.currentTarget.dataset.pickername;
    console.log(name)
    switch (name) {
      case 'major':
        var app = getApp()
        app.major = this.data.majorArr[eindex]
        console.log(app.major)
        this.setData({
          majorIndex: eindex
        })
        break;
      case 'grade':
        var app = getApp()
        app.grade = this.data.gradeArr[eindex]
        console.log(app.grade)
        this.setData({
          gradeIndex: eindex
        })
        break;
      default:
        return
    }
  },

  formSubmit(e) {
    var name = e.detail.value.name;
    var phone = e.detail.value.phone;
    var id = e.detail.value.id;
    var app = getApp()
    var major = app.major;
    var grade = app.grade;
    console.log('form发生了submit事件，携带数据为：', name, phone, id, major, grade)
    wx.login({
      success: function (res) {
        var code1 = res.code
        var app = getApp()
        var appid1 = app.globalData.appid
        var secret1 = app.globalData.secret
        console.log('获取的code', code1, appid1, secret1)
        var ul = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid1 + '&secret=' + secret1 + '&js_code=' + code1 + '&grant_type=authorization_code'
        wx.request({
          url: ul,
          method: 'GET',
          success: function (e) {
            var openid = e.data.openid
            console.log('获取登录身份的唯一openid', openid)
            wx.request({
              url: 'http://localhost:8080/clock_war_exploded/user/save',
              method: 'POST',
              header: {
                'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
              },
              data: {
                openid: openid,
                name: name,
                id: id,
                phone: phone,
                major: app.major,
                grade: app.grade,
              },
              success: function (res) {
                app.getUser()
                wx.navigateBack()
                if (res.data) {
                  wx.showToast({
                    title: '提交成功',
                    icon: 'success',
                  })
                }
              }
            })
          }
        })
      }
    })
  },

  managerLogin() {
    wx.navigateTo({
      url: '/pages/manage/managerLogin/managerLogin',
    })
  }
})